from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

# 配置数据库连接
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:root@localhost:3306/emails'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

class Email(db.Model):
    __tablename__ = 'emails'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text)
    category = db.Column(db.String(50))
    status = db.Column(db.String(20))
    isdeleted = db.Column(db.Boolean, default=False)

    def to_dict(self):
        return {
            'id': self.id,
            'content': self.content,
            'category': self.category,
            'status': self.status,
            'isdeleted': self.isdeleted
        }

@app.route('/emails', methods=['GET'])
def get_emails():
    emails = Email.query.all()
    return jsonify([email.to_dict() for email in emails])

if __name__ == "__main__":
    db.create_all()  # 创建数据库表
